package cn.lyl.basic.controller;

import cn.lyl.basic.dto.PhoneCodeDto;
import cn.lyl.basic.exception.BusinessException;
import cn.lyl.basic.service.IVerifyCodeService;
import cn.lyl.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key")String key){

        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }

    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto dto ){

        try {
            verifyCodeService.getPhoneCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody PhoneCodeDto phoneCodeDto ){
        try {
            verifyCodeService.getBinderSmsCode(phoneCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }

}
